package com.lp.module_home_c.presenter;

import com.lp.module_home_c.api.HomeCApiServer;
import com.lp.module_home_c.bean.TextBean;
import com.lp.module_lib.api.ApiRetrofit;
import com.lp.module_lib.api.BasePresenter;
import com.lp.module_lib.network.file.FileObserver;
import com.lp.module_lib.network.file.FileUtil;
import com.lp.module_lib.network.file.ProgressRequestBody;
import com.lp.module_lib.network.mvp.BaseModel;
import com.lp.module_lib.network.mvp.BaseObserver;
import com.lp.module_lib.network.mvp.BaseView;
import com.lp.module_lib.utils.RetrofitUtil;
import com.luck.picture.lib.entity.LocalMedia;

import java.io.File;
import java.util.HashMap;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.functions.Function;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;

/**
 * 作者：created by LP_TREAM on 2020/11/28 23:27
 * 邮箱：806875201@qq.com
 */

public class HomeCPresenter extends BasePresenter<HomeCPresenter.HomeCView> {
    public HomeCPresenter(HomeCPresenter.HomeCView baseView) {
        super(baseView);
    }

    /**
     * 普通接口请求
     */
    public void getText() {
        HashMap<String, String> params = new HashMap<>();
        params.put("type", "junshi");
        params.put("key", "2c1cb93f8c7430a754bc3ad62e0fac06");
        addDisposable(ApiRetrofit.getInstance(HomeCApiServer.class).getText(params), new BaseObserver(baseView) {
            @Override
            public void onSuccess(BaseModel o) {
                baseView.onTextSuccess(o);
            }

        });
    }

    /**
     * 获取随机笑话
     *
     * @param baseUrl
     */
    public void getJokeList(String baseUrl) {
        HashMap<String, String> params = new HashMap<>();
        params.put("num", "10");
        addDisposable(ApiRetrofit.getBaseUrlInstance(HomeCApiServer.class, baseUrl).getJokeList(params), new BaseObserver(baseView) {
            @Override
            public void onSuccess(BaseModel o) {
                baseView.onJokeListSuccess(o);
            }
        });
    }


    /**
     * 演示 图片和字段一起上传  多图上传（单图把list去掉）
     * <p>
     * 这个接口1 代表成功
     *
     * @param strings
     */
    public void getUpload(List<LocalMedia> strings) {
        HashMap<String, RequestBody> params = new HashMap<>();
        params.put("nickname", RetrofitUtil.convertToRequestBody("奥巴牛"));
        params.put("sn", RetrofitUtil.convertToRequestBody("1541b6d20806"));
        params.put("wx_id", RetrofitUtil.convertToRequestBody("wxid_zlhy64"));
        params.put("msgSvrId", RetrofitUtil.convertToRequestBody("434434543345544444"));
        params.put("create_time", RetrofitUtil.convertToRequestBody(System.currentTimeMillis() / 1000 + ""));

        List<MultipartBody.Part> parts = RetrofitUtil.filesToMultipartBodyParts(RetrofitUtil.initImages(strings), "img");
        addDisposable(ApiRetrofit.getBaseUrlInstance(HomeCApiServer.class, "http://monitoring.qhzxedu.com/").getUpload(params, parts), new BaseObserver(baseView) {
            @Override
            public void onSuccess(BaseModel o) {
                baseView.onUploadSuccess(o);
            }
        });
    }


    /**
     * 演示 文件上传进度监听 展示进度条
     *
     * @param url
     */
    public void getUploadOne(String url) {
        HashMap<String, RequestBody> params = new HashMap<>();
        params.put("fileType", RetrofitUtil.convertToRequestBody("video"));

        MultipartBody.Part parts = MultipartBody.Part.createFormData("file", new File(url).getName(), new ProgressRequestBody(new File(url), "video/mpeg", baseView));

        addDisposable(ApiRetrofit.getBaseUrlInstance(HomeCApiServer.class, "https://bjlzbt.com/").getUploadOne(params, parts), new BaseObserver(baseView, true) {
            @Override
            public void onSuccess(BaseModel o) {
                baseView.onUploadSuccess(o);
            }
        });
    }

    /**
     * 文件下载测试
     *
     * @param url          下载路径
     * @param destFileDir  保存路径
     * @param destFileName 文件命名
     */
    public void downFile(String url, final String destFileDir, final String destFileName) {
        Observable<String> observable = ApiRetrofit.getFileInstance(HomeCApiServer.class, baseView).downloadFile(url).map(new Function<ResponseBody, String>() {
            @Override
            public String apply(ResponseBody body) throws Exception {
                File file = FileUtil.saveFile(destFileDir + "/" + destFileName, body);
                return file.getPath();
            }
        });
        addFileDisposable(observable, new FileObserver(baseView, true) {
            @Override
            public void onSuccess(Object o) {
                baseView.onDownFileSuccess(new File(o.toString()));
            }
        });
    }

    public interface HomeCView extends BaseView {
        void onTextSuccess(BaseModel<TextBean> o);

        void onJokeListSuccess(BaseModel<List<String>> o);

        void onUploadSuccess(BaseModel<Object> o);

        void onDownFileSuccess(File file);
    }
}
